<?php
if (!function_exists("GetSQLValueString")) {
function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "") 
{
  if (PHP_VERSION < 6) {
    $theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;
  }

  $theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);

  switch ($theType) {
    case "text":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;    
    case "long":
    case "int":
      $theValue = ($theValue != "") ? intval($theValue) : "NULL";
      break;
    case "double":
      $theValue = ($theValue != "") ? doubleval($theValue) : "NULL";
      break;
    case "date":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;
    case "defined":
      $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
      break;
  }
  return $theValue;
}
}
require_once('../Connections/sepolwan_connect.php'); 
require 'src/config.php';
require 'src/facebook.php';
// Create our Application instance (replace this with your appId and secret).
$facebook = new Facebook(array(
  'appId'  => $config['1411185599096898'],
  'secret' => $config['fce881bfd08c08feb2d6b0b22f121fc3'],
  'cookie' => true
));
$facebook = new Facebook($config);
  $user_id = $facebook->getUser();
if(isset($_GET['logout']))       
{
    $url = 'https://www.facebook.com/logout.php?next=' . urlencode('http://demo.phpgang.com/facebook_login_graph_api/') .
      '&access_token='.$_GET['tocken'];
    session_destroy();
    header('Location: '.$url);
}
if(isset($_GET['fbTrue']))
{
    $token_url = "https://graph.facebook.com/oauth/access_token?"
       . "client_id=".$config['App_ID']."&redirect_uri=" . urlencode($config['callback_url'])
       . "&client_secret=".$config['App_Secret']."&code=" . $_GET['code']; 

     $response = file_get_contents($token_url);
     $params = null;
     parse_str($response, $params);

     $graph_url = "https://graph.facebook.com/me?access_token=" 
       . $params['access_token'];
	 
     $user = json_decode(file_get_contents($graph_url));
     $extra = "<a href='index.php?logout=1&tocken=".$params['access_token']."'>Logout</a><br>";     
     $content = $user;
	 
	 $colname_sesi_member = "-1";
if (isset($content->{'id'})) {
  $colname_sesi_member = $content->{'id'};
}
mysql_select_db($database_sepolwan_connect, $sepolwan_connect);
$query_sesi_member = sprintf("SELECT * FROM memberdb WHERE email = %s", GetSQLValueString($colname_sesi_member, "text"));
$sesi_member = mysql_query($query_sesi_member, $sepolwan_connect) or die(mysql_error());
$row_sesi_member = mysql_fetch_assoc($sesi_member);
$totalRows_sesi_member = mysql_num_rows($sesi_member);
	if ($totalRows_sesi_member==0){ 
	 $insertSQL = sprintf("INSERT INTO memberdb (id, full_name, nama_user, email, password, aktif, about, tempat, website) VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s)",
                       GetSQLValueString('', "int"),
                       GetSQLValueString(preg_replace("/[^a-z0-9\\040\\.\\-\\_\\\\]/i", "", $content->{'name'}), "text"),
                       GetSQLValueString(str_replace('.', '_', $content->{'username'}), "text"),
                       GetSQLValueString($content->{'id'}, "text"),
                       GetSQLValueString('facebook', "text"),
                       GetSQLValueString('ya', "text"),
                       GetSQLValueString($content->{'bio'}, "text"),
                       GetSQLValueString($content->{'hometown'}->{'name'}, "text"),
                       GetSQLValueString('', "text"));

  mysql_select_db($database_sepolwan_connect, $sepolwan_connect);
  $Result1 = mysql_query($insertSQL, $sepolwan_connect) or die('Maaf, Terjadi Kesalahan Input Data.. <br /> silahkan coba lagi..');
	}
  $_SESSION['MM_Username']=$content->{'id'};
  
  header("Location: http://geriloji.com/index.php" );
}
else
{
    $content = '<a href="https://www.facebook.com/dialog/oauth?client_id='.$config['App_ID'].'&redirect_uri='.$config['callback_url'].'&scope=email,user_likes,publish_stream"><img src="./images/login-button.png" alt="Sign in with Facebook"/></a>';
}
?>